package com.zuocheng.librarycar;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context mContext;
    private List<CarBean> mDatas;

    public RecyclerAdapter(Context context, List<CarBean> datas) {
        mContext = context;
        mDatas = datas;
    }

    @NonNull
    @NotNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_layout, parent, false);
        return new NormalHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull RecyclerView.ViewHolder holder, int position) {
        CarBean item = mDatas.get(position);
        NormalHolder normalHolder = (NormalHolder) holder;
        normalHolder.carBean = item;
        normalHolder.mTV.setText(item.createTime);
        normalHolder.tvStatus.setText(item.statusName);
        normalHolder.tvName.setText(item.carBrand + item.carModel + item.color);
        normalHolder.tvAuctionContet.setText(item.carBrand +"  " + item.carModel +"  "+ item.color);
        normalHolder.tvPrice.setText(item.vinNo);
        normalHolder.tvAuctionTime.setText(item.addressName);


        if(item.carImgFile != null && item.getCarImgFile().length() > 0) {
            String[] imageArr = item.getCarImgFile().split(",");
            List<String> imageList = Arrays.asList(imageArr);
//            if(imageList != null && imageList.size() > 3) {
//                imageList = imageList.subList(0,3);
//            }
            System.out.println("======" +imageList);
            Glide.with(mContext).load(imageList.get(0)).into(normalHolder.ivImage);
            String carUrl = "https://tenfei04.cfp.cn/creative/vcg/veer/800water/veer-150817358.jpg";
            Glide.with(mContext).load(carUrl).into(normalHolder.ivImage);
        }


    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }


    public class NormalHolder extends RecyclerView.ViewHolder{
        public CarBean carBean;
        public TextView mTV;
        public TextView tvStatus;
        public ImageView ivImage;
        public LinearLayout backLinear;
        public TextView tvName;
        public TextView tvAuctionContet;
        public TextView tvPrice;
        public TextView tvAuctionTime;

        public NormalHolder(View itemView) {
            super(itemView);
            mTV = (TextView) itemView.findViewById(R.id.item_tv);
            tvStatus = (TextView) itemView.findViewById(R.id.tv_status);
            ivImage =(ImageView) itemView.findViewById(R.id.iv_image);
            backLinear = itemView.findViewById(R.id.tv_backGround);
            tvName = itemView.findViewById(R.id.tv_name);
            tvAuctionContet = itemView.findViewById(R.id.tv_auction_contet);
            tvPrice = itemView.findViewById(R.id.tv_price);
            tvAuctionTime = itemView.findViewById(R.id.tv_auction_time);

            backLinear.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //Toast.makeText(mContext,mTV.getText(),Toast.LENGTH_SHORT).show();
                    String json = JSON.toJSONString(carBean);
                    CarDetailActivity.start(mContext,json);
                }
            });
        }
    }

}
